arc != uname -m | grep -e x86_64 -e amd64 | wc -c
.if( $(arc) != 0 )
.error invalid architecture
.endif

#
FC  = gfortran
LD  = gfortran
CPP = cpp
ASM = nasm
FFLAGS = -cpp -pipe -fsecond-underscore -x f95 \
	-c -Wall -std=gnu -fno-range-check -Wno-conversion \
	-fbackslash
AFLAGS = -f elf
CPPFLAGS = -C

#
.ifdef _DEBUG_
FFLAG += -g
.else
FFLAG += -s -fomit-frame-pointer -O3
.endif

#
.ifdef _LINUX_ 
AFLAGS += -D_LINUX_
.endif

#
# asm source codes
#
liba_src= \
mutyA.asm \
kutyA.asm \
futyA.asm \
cutyA.asm \
anaA.asm
#
# source codes
#
libf_src= \
mutyF.f90 \
futyF.f90 \
kutyF.f90 \
butyF.f90 \
butyD.f90 \
butyM.f90 \
cutyF.f90 \
putyF.f90 \
optimizer.f90 \
ana.f90 \
MatchMaker.f90
#
# mod file is automatically created and the name is 
# converted to small letters.
#
libf_inc= \
muty.mod \
futy.mod \
kuty.mod \
buty.mod \
butyd.mod \
butyr8.mod \
butyr4.mod \
butyi2.mod \
butyi4.mod \
butyi1.mod \
cuty.mod \
puty.mod \
optimizer.mod \
ana.mod \
matchmaker.mod
#
# object code of asm 
#
liba_obj=\
mutyA.o \
kutyA.o \
futyA.o \
cutyA.o \
anaA.o
#
# object code of fortran 
# the order is important 
#
libf_obj= \
cutyF.o \
butyD.o \
mutyF.o \
futyF.o \
kutyF.o \
butyM.r8.o \
butyM.r4.o \
butyM.i2.o \
butyM.i4.o \
butyM.i1.o \
butyF.o \
putyF.o \
optimizer.o \
ana.o \
MatchMaker.o

.PHONY: clean all

.SUFFIXES:
.SUFFIXES: .o .asm .f90 .mod

all: libanam.a libanam.so

.f90.o:
	$(FC) $(FFLAGS) $<

.f90.mod:
	$(FC) $(FFLAGS) $<

.asm.o:
	$(ASM) $(AFLAGS) $<

cutyF.o: cutyF.f90
cuty.mod: cutyF.o
futyF.o: futyF.f90
kutyF.o: kutyF.f90
mutyF.o: mutyF.f90
butyD.o: butyD.f90
butyd.mod: butyD.o
butyr8.mod: butyM.r8.o
butyr4.mod: butyM.r4.o
butyi4.mod: butyM.i4.o
butyi2.mod: butyM.i2.o
butyi1.mod: butyM.i1.o
butyF.o: butyF.f90 butyd.mod \
	butyr4.mod butyr8.mod butyi4.mod butyi2.mod butyi1.mod \
	cuty.mod
ana.o: ana.f90 cuty.mod
putyF.o: putyF.f90
optimizer.o: optimizer.f90

butyM.i4.f90: butyM.f90
	$(CPP) $(CPPFLAGS) -D_IKIND_=4 butyM.f90 -o tmp
	sed "s/@/_/" tmp > $@ && \rm -f tmp

butyM.r8.f90: butyM.f90
	$(CPP) $(CPPFLAGS) -D_RKIND_=8 butyM.f90 -o tmp
	sed "s/@/_/" tmp > $@ && \rm -f tmp

butyM.r4.f90: butyM.f90
	$(CPP) $(CPPFLAGS) -D_RKIND_=4 butyM.f90 -o tmp
	sed "s/@/_/" tmp > $@ && \rm -f tmp

butyM.i2.f90: butyM.f90
	$(CPP) $(CPPFLAGS) -D_IKIND_=2 butyM.f90 -o tmp
	sed "s/@/_/" tmp > $@ && \rm -f tmp

butyM.i1.f90: butyM.f90
	$(CPP) $(CPPFLAGS) -D_IKIND_=1 butyM.f90 -o tmp
	sed "s/@/_/" tmp > $@ && \rm -f tmp

libanam.a:  $(liba_obj) $(libf_obj)
	ar r $@ $>
	ranlib $@

libanam.so: $(liba_obj)  $(libf_obj)
	$(LD) -shared $> -o $@

clean:
	\rm -f *.o *.*~ *.core *.a *.mod libanam.a libanam.so
